<template>
	<view class="container">
		<view class="search-param">
			<uni-search-bar placeholder="请输入客户名称" @confirm="search" @cancel="show" v-model="sapOrderForm.customerName">
			</uni-search-bar>
		</view>

		<view>
			<uni-row >
				<uni-col :span="12">
					<view class="datetime-picker-container">
						<uni-datetime-picker v-model="synchParams.begda" type="date"  
							placeholder="选择开始时间" @change="search"></uni-datetime-picker>
					</view>
				</uni-col>
				<uni-col :span="12">
					<view class="datetime-picker-container">
						<uni-datetime-picker v-model="synchParams.endda" type="date" 
							placeholder="选择结束时间" @change="search"></uni-datetime-picker>
					</view>
				</uni-col>
			</uni-row>
		</view>

		<view class="search-results-container">

			<view v-for="(custName, index) in searchResults" :key="index" class="search-result"
				@tap="goToDetailPage(custName)">{{ custName }}</view>

			<!--<uni-load-more :status="status" :content-text="contentText" />-->
		</view>

		<view style="position: fixed; bottom: 0px; width: 100%;">
			<uni-row>
				<uni-col :span="12">
					<view>
						<button @click="goToHome">返回首页</button>
					</view>
				</uni-col>
				<uni-col :span="12">
					<view>
						<button @click="submitFormSynch2" v-preventReClick='4000'>同步订单</button>
					</view>
				</uni-col>
			</uni-row>
		</view>

	</view>
</template>

<script>
	import {
		getCustomerNameList,
		getNameListByName,
		orderSynchronization,
		orderSyncByOptName,
		statusSyncByOptName,
	} from '@/api/tms/saleOrder'

	export default {
		created() {
			const yesterday = new Date();  
			yesterday.setDate(yesterday.getDate() - 1);
			this.statusSync();
			this.submitFormSynch({
				begda: this.formatDate(yesterday),
				endda: this.formatDate(new Date())
			});
	        const fifteenDaysAgo = new Date();
	        fifteenDaysAgo.setDate(fifteenDaysAgo.getDate() - 15);
			this.synchParams.begda = fifteenDaysAgo;
			this.synchParams.endda = new Date();
			console.log('created');
			console.log(this.synchParams);
		},
		data() {
			return {
				searchResults: [],
				showResults: false,
				sapOrderForm: {
					customerName: "",
					begda: null,
					endda: null,
					dataLoaded: false,
				},
				// 同步参数
				synchParams: {
					begda: null,
					endda: null,
				},
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				page: 1,
				limit: 50,
			};
		},
		methods: {
			formatDate(date) {
				const year = date.getFullYear().toString(); // 获取年份并转为字符串类型
				const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份（注意需加上1）并补零到两位数
				const day = date.getDate().toString().padStart(2, '0'); // 获取天数并补零到两位数
				return year + month + day
			},
			formatDateToYYYYMMDD(input) {
						    let date;
						    
						    // 尝试将输入转换为Date对象
						    if (input instanceof Date) {
						        date = input;
						    } else if (typeof input === 'string') {
						        date = new Date(input);
						    } 
						    const year = date.getFullYear();
						    const month = String(date.getMonth() + 1).padStart(2, '0'); // 确保月份是两位数
						    const day = String(date.getDate()).padStart(2, '0'); // 确保日期是两位数
						    return `${year}${month}${day}`;
			},
			syncOrders() {
				// 同步订单逻辑  
			},
			goToHome() {
				uni.switchTab({
					url: '/pages/work/index',
				});
			},
			search() {
				console.log('search');
				console.log({
					"pageNum": 1,
					"pageSize": 300,
					"customerName": this.sapOrderForm.customerName,
					"begda": this.synchParams.begda,
					"endda": this.synchParams.endda
				});
				if (!this.synchParams.begda) {
				    this.$modal.msgError("起始时间必填");
				}
				if (!this.synchParams.endda) {
				    this.$modal.msgError("结束时间必填");
				}
				if (this.synchParams.begda && this.synchParams.endda) {
					this.show();
				}
			},
			show() {
				console.log('show');
				console.log({
					"pageNum": 1,
					"pageSize": 300,
					"customerName": this.sapOrderForm.customerName,
					"begda": this.synchParams.begda,
					"endda": this.synchParams.endda
				});
				if (!this.synchParams.begda) {
				   const fifteenDaysAgo = new Date();
				   fifteenDaysAgo.setDate(fifteenDaysAgo.getDate() - 15);
				   this.synchParams.begda = fifteenDaysAgo;
				}
				if (!this.synchParams.endda) {
				   this.synchParams.endda = new Date();
				}
				console.log({
					"pageNum": 1,
					"pageSize": 300,
					"customerName": this.sapOrderForm.customerName,
					"begda": this.synchParams.begda,
					"endda": this.synchParams.endda
				});
				getCustomerNameList({
					"pageNum": 1,
					"pageSize": 300,
					"customerName": this.sapOrderForm.customerName,
					"begda": this.synchParams.begda,
					"endda": this.synchParams.endda
				}).then(response => {
					this.searchResults = response.rows;
				});
			},
			onLoad() {
				this.show();
			},
			goToDetailPage(custName) {
				uni.navigateTo({
					url: '/pages/ec/saleOrderDetail/index?custName=' + custName,
				});
			},
			submitFormSynch(synchParams) {
				orderSyncByOptName(synchParams).then(response => {
					this.$modal.msgSuccess("同步成功");
				});
				this.search();
			},
		    statusSync() {
				console.log("同步");
				try {
					statusSyncByOptName().then(response => {
						this.$modal.msgSuccess("状态同步成功");
						this.openSynch = false;
					});
				} catch (error) {
					console.error("状态同步失败:", error);
				}
			},
			submitFormSynch2() {
				let p = {}
				p.begda = this.formatDateToYYYYMMDD(this.synchParams.begda);
				p.endda = this.formatDateToYYYYMMDD(this.synchParams.endda);
				orderSynchronization(p).then(response => {
					this.$modal.msgSuccess("同步成功");
				});
				this.search();;
			},
			getUniversityList() {
				getCustomerNameList({
					"pageNum": this.page,
					"pageSize": 50,
					"customerName": this.sapOrderForm.customerName
				}).then(res => {
					if (this.page === 1) {
						this.searchResults = res.rows;
					} else {
						this.searchResults = this.searchResults.concat(res.rows);
					}
					// 判断是否还有更多数据
					if (res.rows.length < this.limit) {
						this.status = 'noMore'; // 没有更多数据
					} else {
						this.status = 'more'; // 还有更多数据
					}
				});
			},
		},
	};
</script>

<style>
	.container {
		dispaly: flex;
		flex-direction: row;
		flex: auto;
		background-color: #fff;
	}

	.search-param {
		background-color: #fff;
	}

	.datetime-picker-container {
		flex: 1;
		text-align: center;
		/* 将内容设置为居中 */
	}

	.search-results-container {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 200%;
		/* 设置高度为内容的高度的两倍 */
		padding: 3px;
		/* 设置内边距 */
		background-color: #fff;
		/* 设置背景色 */
		margin-bottom: 120px;
		padding-bottom: 120px;
	}

	.search-result {
		flex: 1;
		/* 使每个内部元素均等分布 */
		margin-bottom: 10px;
		/* 设置底部边距 */
		background-color: #fff;
		border-top: 4px solid #f5f5f5;
		/* 设置底部边框颜色 */
		padding: 4px;
		/* 设置内边距 */
	}

	.uni-datetime-picker-active {
		margin-bottom: 40px;
	}
</style>